package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqCompostProduceFaultMapper;
import com.guofen.pojo.SjqCompostProduceElectricity;
import com.guofen.service.sjq.SjqCompostProduceInspectionService;
import com.guofen.pojo.SjqCompostProduceFault;
import com.guofen.service.sjq.SjqCompostProduceFaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqCompostProduceFaultServiceImpl implements SjqCompostProduceFaultService {
    @Autowired
    SjqCompostProduceFaultMapper sjqCompostProduceFaultMapper;
    @Autowired
    SjqCompostProduceInspectionService sjqCompostProduceInspectionService;

    @Override
    public boolean add(String inspectionId,
                       String equipmentName,
                       String faultTime,
                       String operator,
                       String faultContent,
                       String serviceTimeStart,
                       String serviceTimeEnd,
                       String servicemanInCharge,
                       String serviceman,
                       String faultReason,
                       String serviceMethod,
                       String proposer,
                       String recoverTime,
                       String inspector){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String eReadingTime = timeFormat.format(now);

        SjqCompostProduceFault sjqCompostProduceFault = new SjqCompostProduceFault();
        sjqCompostProduceFault.setDate(date);
        sjqCompostProduceFault.setEquipmentName(equipmentName);
        sjqCompostProduceFault.setFaultTime(faultTime);
        sjqCompostProduceFault.setOperator(operator);
        sjqCompostProduceFault.setFaultContent(faultContent);
        sjqCompostProduceFault.setServiceTimeStart(serviceTimeStart);
        sjqCompostProduceFault.setServiceTimeEnd(serviceTimeEnd);
        sjqCompostProduceFault.setServicemanInCharge(servicemanInCharge);
        sjqCompostProduceFault.setServiceman(serviceman);
        sjqCompostProduceFault.setFaultReason(faultReason);
        sjqCompostProduceFault.setServiceMethod(serviceMethod);
        sjqCompostProduceFault.setProposer(proposer);
        sjqCompostProduceFault.setRecoverTime(recoverTime);
        sjqCompostProduceFault.setInspector(inspector);

        int result = sjqCompostProduceFaultMapper.insert(sjqCompostProduceFault);
        boolean result2 = sjqCompostProduceInspectionService.set_is_from(inspectionId);
        if (result == 1)
            if(result2){
                return true;
            }else{
                return false;
            }
        else
            return false;
    }

    @Override
    public boolean addFrontPart(String equipmentName, String faultTime, String operator, String faultContent) {
        SjqCompostProduceFault sjqCompostProduceFault = new SjqCompostProduceFault();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);
        sjqCompostProduceFault.setDate(date);
        sjqCompostProduceFault.setEquipmentName(equipmentName);
        sjqCompostProduceFault.setFaultTime(faultTime);
        sjqCompostProduceFault.setOperator(operator);
        sjqCompostProduceFault.setFaultContent(faultContent);
        int result = sjqCompostProduceFaultMapper.insertSelective(sjqCompostProduceFault);
        if (result == 1)
            return true;
        return false;
    }

    @Override
    public boolean addAfterPart(Integer faultId, String serviceTimeStart, String serviceTimeEnd,
                                String servicemanInCharge, String serviceman, String faultReason, String serviceMethod,
                                String proposer, String recoverTime, String inspector) {
        SjqCompostProduceFault sjqCompostProduceFault = new SjqCompostProduceFault();
        sjqCompostProduceFault.setId(faultId);
        sjqCompostProduceFault.setServiceTimeStart(serviceTimeStart);
        sjqCompostProduceFault.setServiceTimeEnd(serviceTimeEnd);
        sjqCompostProduceFault.setServicemanInCharge(servicemanInCharge);
        sjqCompostProduceFault.setServiceman(serviceman);
        sjqCompostProduceFault.setFaultReason(faultReason);
        sjqCompostProduceFault.setServiceMethod(serviceMethod);
        sjqCompostProduceFault.setProposer(proposer);
        sjqCompostProduceFault.setRecoverTime(recoverTime);
        sjqCompostProduceFault.setInspector(inspector);

        int res1 = sjqCompostProduceFaultMapper.updateByPrimaryKeySelective(sjqCompostProduceFault);
//        boolean res2 = sjqCompostProduceInspectionService.set_is_from(inspectionId);
        if (res1 == 1)
            return true;
        return false;
    }

    @Override
    public SjqCompostProduceFault getlast_day(){
        return sjqCompostProduceFaultMapper.getlast();
    }

    @Override
    public SjqCompostProduceFault getLastByEquipmentName(String equipmentName) {
        return sjqCompostProduceFaultMapper.getLastByEquipmentName(equipmentName);
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqCompostProduceFault> list = sjqCompostProduceFaultMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqCompostProduceFault> list = sjqCompostProduceFaultMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqCompostProduceFaultMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqCompostProduceFault record) {
        return sjqCompostProduceFaultMapper.updateByPrimaryKeySelective(record);
    }


}
